/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.mai.antexpress.dao.DesignationDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.DesignationEntity;
import team.mai.antexpress.entities.PermissionGroupEntity;

import java.util.List;

@Service
public class DesignationService {

	public static final int SUCCESS = 0;
	public static final int ERROR = -1;
	public static final int INSUFFICIENT_PRIVILEGE = -2;
	public static final int USER_IN_DESIGNATION = -3;

	@Autowired
	private DesignationDAO designationDAO;
	@Autowired
	private UserDAO userDAO;

	public int insertDesignation(String operatorName, DesignationEntity dsgt) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDesignation()) {
			return designationDAO.insertDesignation(dsgt);
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}
	public int deleteDesignation(String operatorName, int dsgtId) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDesignation()) {
			if(designationDAO.hasUserInDesignation(dsgtId)){
				return USER_IN_DESIGNATION;
			}
			if (designationDAO.deleteDesignation(dsgtId)) {
				return SUCCESS;
			} else {
				return ERROR;
			}

		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}
	public int deleteUsersInDesignation(String operatorName, int dsgtId){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDesignation()) {
			if (designationDAO.deleteUsersInDepartment(dsgtId)) {
				return SUCCESS;
			} else {
				return ERROR;
			}

		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}
	public int updateDesignation(String operatorName, DesignationEntity dsgt) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDesignation()) {
			if (designationDAO.updateDesignation(dsgt)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}

	}
	public List<DesignationEntity> getAllDesignations(String operatorName) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isLookupDesignation()) {
			return designationDAO.getAllDesignations();
		} else {
			return null;
		}

	}
	public DesignationEntity getDesignation(String operatorName, Integer id) {
		if (id == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupDesignation()) {
				return designationDAO.getDesignationById(id);
			} else {
				return null;
			}
		}
	}


}
